﻿using System;
using System.Configuration;

namespace Sustainalytics.ESGRatings.Computing.Service.Helpers
{
    // Define a custom section.
    // The CustomSection type allows to define a custom section
    // programmatically.
    public sealed class ServiceAutoProvisioningSection :
        ConfigurationSection
    {
        // The connectionString property.
        private static readonly ConfigurationProperty _ConnectionString = new ConfigurationProperty(
                                                                                "connectionString",
                                                                                 typeof(string), 
                                                                                 string.Empty,
                                                                                 ConfigurationPropertyOptions.IsRequired);

        // The setId property.
        private static readonly ConfigurationProperty _SetId = new ConfigurationProperty(
                                                                    "setId",
                                                                    typeof(Guid), 
                                                                    Guid.Empty);

        // The setId property.
        private static readonly ConfigurationProperty _GetLatestSet = new ConfigurationProperty(
                                                                            "getLatestSet",
                                                                            typeof(bool),
                                                                            true);

        // The collection (property bag) that contains
        // the section properties.
        private static ConfigurationPropertyCollection _properties;

        // Internal flag to disable
        // property setting.
        private static bool _readOnly;

        // CustomSection constructor.
        public ServiceAutoProvisioningSection()
        {
            // Property initialization
            _properties = new ConfigurationPropertyCollection { _ConnectionString, _SetId, _GetLatestSet };
        }

        public string ConnectionString
        {
            get
            {
                return (string)this["connectionString"];
            }

            set
            {
                // With this you disable the setting.
                // Remember that the _ReadOnly flag must
                // be set to true in the GetRuntimeObject.
                this.ThrowIfReadOnly("connectionString");
                this["connectionString"] = value;
            }
        }

        public Guid SetId
        {
            get
            {
                return Guid.Parse(this["setId"].ToString());
            }

            set
            {
                this["setId"] = value;
            }
        }

        public bool GetLatestSet
        {
            get
            {
                return bool.Parse(this["getLatestSet"].ToString());
            }

            set
            {
                this["getLatestSet"] = value;
            }
        }

        // This is a key customization.
        // It returns the initialized property bag.
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }

        private new bool IsReadOnly
        {
            get
            {
                return _readOnly;
            }
        }

        // Customizes the use of CustomSection
        // by setting _ReadOnly to false.
        // Remember you must use it along with ThrowIfReadOnly.
        protected override object GetRuntimeObject()
        {
            // To enable property setting just assign true to
            // the following flag.
            _readOnly = true;
            return base.GetRuntimeObject();
        }

        // Use this to disable property setting.
        private void ThrowIfReadOnly(string propertyName)
        {
            if (this.IsReadOnly)
            {
                throw new ConfigurationErrorsException("The property " + propertyName + " is read only.");
            }
        }
    }
}